package org.fatmansoft.teach.repository;

import org.fatmansoft.teach.models.HomeWork;
import org.fatmansoft.teach.models.StudentHomeWork;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Optional;

public interface StudentHomeWorkRepository extends JpaRepository<StudentHomeWork, Integer> {

    @Query(value = "select max(StudentHomeWorkId)from StudentHomeWork ")
    Integer getMaxId();

   @Query(value = "from StudentHomeWork where student.person.num like %?1% and homeWork.homeWorkName like %?2% and homeWork.homeWorkTime like %?3%")
    Optional<StudentHomeWork> getStudentHomeWork(String num, String name, String time);

    @Query(value = "from StudentHomeWork where student.person.num like %?1% and  homeWork.homeWorkName like %?2% and homeWork.homeWorkTime like %?3% ")
    List<StudentHomeWork> getStudentHomeWorkListUnique(String num, String name, String time);

    @Query(value = "from StudentHomeWork  where homeWork.homeWorkName like %?1% and homeWork.homeWorkTime like ?2% ")
    List<StudentHomeWork> getStudentList(String name, String time);

    @Query(value = "from StudentHomeWork where student.person.num like %?1% and (homeWork.homeWorkName like %?2% or homeWork.homeWorkTime like %?2% )")
    List<StudentHomeWork> getHomeWorkList(String num, String nameTime);

 @Modifying@Query(value = "delete from StudentHomeWork where ?1='' and homeWork =?2 ")
    void deleteByHomeWork(String studentName,HomeWork homeWork);
}
